/*--App--*/
var _self = this, _table = null, _tabControl = null;
imagef = ['gif', 'jpg', 'jpeg', 'png', 'bmp'];
var _html;
var _ff;
var _click_tr;

_self.index = function(search, data) {
    var a = this;
    _table = new $.Form.Table({
        module: a.module,
        app: a.app,
        rowNum: true,
        bAutoCallBack: true,
        oTableButtons: {
            add: "add"
        },
        action: {
            'add_group': function(nTd, sData, oData, iRow, iCol) {
                $.request('/' + a.app + '/' + a.module + '/add_group?id=' + oData['id']);
            },
            edit: ["id"],
            delete: function(nTd, sData, oData, iRow, iCol) {
                $.delete_ask(a.A('do_delete'), {id: oData.id});
            },
            'picture': function(nTd, sData, oData, iRow, iCol) {
                $.request('/' + a.app + '/picture/index?id=' + oData['id']);
            },
//            'set_top': function (nTd, sData, oData, iRow, iCol) {
//                $.request('/' + a.app + '/' + a.module + '/set_top?id=' + oData['id']);
//            },
            'set_sort': function(nTd, sData, oData, iRow, iCol) {
                $.request('/' + a.app + '/' + a.module + '/set_sort?id=' + oData['id']);
            },
            send_message: ["id"],
//            upload: ['id'],
            
        },
        actionRender: {
            add_group: function(nTd, sData, oData, iRow, iCol) {
                if (oData.groupid * 1 > 0) {
                    return false;
                }
                return true;
            }
        },
        pk: "id",
        aaSorting: [['time', 'desc']],
        oTableSearch: {
            content: search,
            clear: true
        },
        thead: {
            id: {
                sClass: "center",
                bVisible: false
            },
            'sort': {
                sClass: "center"
            },
            active_type: {
                sClass: "center"
            },
            sponsor: {
                sClass: "center"
            },
            title: {
                sClass: "center details",
                fnClick: function() {
                    _click_tr = $(this).parent("tr")[0];
                    var data = _table.DataTable.fnGetData(_click_tr);
                    if ($(this).parent().is("thead"))
                        return;
                    if (_table.DataTable.fnIsOpen(_click_tr)) {
                        _table.DataTable.fnClose(_click_tr);
                    } else
                    {
                        $.request(a.A('details'), {id: data.id}, {trigger: _click_tr});
                    }
                }
            },
            /*content: {
             sClass: "center"
             },*/
            img: {
                sClass: "center nowrap",
                fnRender: function(nTd, sData, oData, iRow, iCol) {
                    var obj;
                    if (sData === '') {
                        obj = $($.html_icon('add_image'));
                        $(nTd).html(obj);
                    } else
                    {
                        this.dataTableExt.type.sType['tipimage'].call(this, nTd, sData, oData, iRow, iCol);

                        $(nTd).addClass('details');
                        obj = $(nTd);
                    }
                    obj && obj.on('click', function() {
                        $.request('/main/resource/dialog:active_image_path_', {width: 100, height: 100, tools: {file: ['link']}, path: oData.image || $.cookie(a.L("resource"))}, {
                            options: {
                                onRead: function(path) {
                                    //$.cookie(a.L("resource"), path);
                                },
                                onLink: function(resource) {
                                    var obj = this;
                                    if ($.inArray(resource.ext, imagef)) {
                                        $.request(a.A('do_add_image'), {
                                            id: oData.id,
                                            image: resource.filepath
                                        },
                                        {
                                            callback: function() {
                                                _table.DataTable.fnDraw(_table.DataTable.dataTableSettings);
                                                obj.Close();
                                            },
                                            autoCallBack: true,
                                            trigger: _table
                                        });
                                    } else
                                    {
                                        $.error('error_file_ext');
                                    }
                                }
                            }
                        });
                    });
                }
            },
            image: {
                sClass: "center nowrap",
                fnRender: function(nTd, sData, oData, iRow, iCol) {
                    var obj;
                    if (sData === '') {
                        obj = $($.html_icon('add_image_'));
                        $(nTd).html(obj);
                    } else
                    {
                        this.dataTableExt.type.sType['tipimage2'].call(this, nTd, sData, oData, iRow, iCol);

                        $(nTd).addClass('details');
                        obj = $(nTd);
                    }
                    obj && obj.on('click', function() {
                        $.request('/main/resource/dialog:active_image_path_', {width: 100, height: 100, tools: {file: ['link']}, path: oData.image || $.cookie(a.L("resource"))}, {
                            options: {
                                onRead: function(path) {
                                    //$.cookie(a.L("resource"), path);
                                },
                                onLink: function(resource) {
                                    var obj = this;
                                    if ($.inArray(resource.ext, imagef)) {
                                        $.request(a.A('do_add_image_'), {
                                            id: oData.id,
                                            image: resource.filepath
                                        },
                                        {
                                            callback: function() {
                                                _table.DataTable.fnDraw(_table.DataTable.dataTableSettings);
                                                obj.Close();
                                            },
                                            autoCallBack: true,
                                            trigger: _table
                                        });
                                    } else
                                    {
                                        $.error('error_file_ext');
                                    }
                                }
                            }
                        });
                    });
                }
            },
            b_time: {
                sClass: "center",
                sType: "datetime",
            },
            e_time: {
                sClass: "center",
                sType: "datetime",
            },
            time: {
                sClass: "center",
                sType: "date",
            },
            praise: {
                sClass: "center"
            },
            money: {
                sClass: "center"
            },
            address: {
                sClass: "center"
            },
            number: {
                sClass: "center"
            },
//            user_name: {
//                sClass: "center"
//            },
            active_status: {
                sClass: "center"
            }
        }
    });

    _tabControl = $.TabControl.addControl({app: a.app, module: a.module, action: a.action, Control: _table});

};

//_self.edit = _self.add = function(html) {
//    var $html = $(html);
//    $.dialogForm($html, this.lang);
//    //$.Log( $('[data-action]',$html));  ;
//
//};
var __editor = 0;
_self.add = _self.edit = function(html) {
    var a = this, _html = $(html);
    $.dialogForm(_html, this.lang, function() {
    }, function() {
        um && um.destroy();
    });
    var id = '__editor' + __editor++;
    $('#__editor', _html).attr('id', id);
    $('#__editor', _html);
    var serverPath = '/plugins/umeditor/php/',
            um = UM.getEditor(id, {
                imageUrl: serverPath + "imageUp.php",
                imagePath: serverPath,
                lang: /^zh/.test(navigator.language || navigator.browserLanguage || navigator.userLanguage) ? 'zh-cn' : 'en',
                langPath: UMEDITOR_CONFIG.UMEDITOR_HOME_URL + "lang/",
                focus: true,
                autoHeightEnabled: false,
                autoFloatEnabled: false,
            });


};
_self.set_sort = _self.send_message = function(html) {
    var a = this, _html = $(html);
    $.dialogForm(_html, this.lang);
}

_self.do_set_sort = _self.do_delete = _self.add_group = function() {
    _table.DataTable.fnDraw(_table.DataTable.dataTableSettings);
};
_self.do_edit = _self.do_add = _self.do_add_image = _self.do_send_message = function() {
    if (this.dialog)
        this.dialog.modal('hide');
    _table.DataTable.fnDraw(_table.DataTable.dataTableSettings);
};

_self.get_image = function(html) {
    _f = $(html);
    var f = this;

    bootbox.dialog({
        message: _f,
        title: $.lang("身份证照片"),
        buttons: {
            cancel: {
                label: $.lang('关闭'),
                className: "no-border btn-danger"
            }
        }
    });
};

_self.details = function(data) {
    var _f = $(data), a = this;
    var data = _table.DataTable.fnGetData(a.trigger);
    _self.details[data.id] = _f;
    _table.DataTable.fnOpen(a.trigger, _f, "info_row");
};
this.upload = function(data, id) {
    _html = $(data);
    f = this;

    bootbox.dialog({
        message: _html,
        title: $.lang(f.L("upload")),
        buttons: {
            cancel: {
                label: $.lang(f.L("close")),
                className: "no-border btn-danger"
            },
            confirm: {
                label: $.lang(f.L("save")),
                className: "no-border btn-success",
                callback: function() {
                    _html.trigger("submit");
                    return true;
                }
            }
        }
    });
//    $.dialogForm(_html, this.lang);
    $("#uploadify").uploadify({
        buttonText: "浏览图片",
        height: 28,
        swf: 'plugins/jquery.uploadify/3.2.1/uploadify.swf',
        uploader: f.A('do_upload') + '?id=' + id,
        width: 80,
        // auto: false,
        // removeCompleted: false,
        removeTimeout: 0,
        // debug : true
        // 上传完毕:
        queueID: "fileQueue",
        fileTypeExts: "*.jpg;*.gif;*.png;*.jpeg",
        fileSizeLimit: "3145728",
        // 上传成功
        onUploadSuccess: function(a, b, c) {

            var d = eval('(' + b + ')');
//            $.Debug(d.url);

            $("#image").val(d.name);
            $("#img").attr('src', d.url);

        },
        // 上传发生错误
        onUploadError: function(a, b, c) {
            /*alert("onUploadError");
             alert( 'id: ' + a.id
             + ' - 索引: ' + a.index
             + ' - 文件名: ' + a.name
             + ' - 文件大小: ' + a.size
             + ' - 类型: ' + a.type
             + ' - 创建日期: ' + a.creationdate
             + ' - 修改日期: ' + a.modificationdate
             + ' - 文件状态: ' + a.astatus
             + ' - 错误代码: ' + b
             + ' - 错误描述: ' + c );*/
        },
        onCancel: function(a, b, c) {
            //alert("onCancel");
        },
        onSelect: function(e) {
            /*
             * e.post.type = e.type; alert("唯一标识:" + e.id + "\r\n" + "文件名：" + e.name +
             * "\r\n" + "文件大小：" + e.size + "\r\n" + "创建时间：" + e.creationdate +
             * "\r\n" + "最后修改时间：" + e.modificationdate + "\r\n" + "文件类型：" + e.type );
             */
            //showMsg({msg: "", type: "tip", tag: "#uploadify-button"});
            //showMsg({msg: "", type: "tip", tag: "#uploadify-button"});
        }
    });
};
/*--/App--*/